/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utils;

import java.io.File;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.mail.internet.ContentType;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author user
 */
public class Utils {

    public static String md5(String input) {
        try {

            MessageDigest m = MessageDigest.getInstance("MD5");
            byte[] data = input.getBytes();
            m.update(data, 0, data.length);
            BigInteger i = new BigInteger(1, m.digest());
            return String.format("%1$032X", i);

        } catch (Exception ex) {
            return "";
        }
    }

    public static ArrayList<ArrayList<String>> extract(ResultSet resultSet)
            throws SQLException {
        ArrayList<ArrayList<String>> table;
        int columnCount = resultSet.getMetaData().getColumnCount();

        if (resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY) {
            table = new ArrayList<ArrayList<String>>();
        } else {
            resultSet.last();
            table = new ArrayList<ArrayList<String>>(resultSet.getRow());
            resultSet.beforeFirst();
        }

        for (ArrayList<String> row; resultSet.next(); table.add(row)) {
            row = new ArrayList<String>(columnCount);

            for (int c = 1; c <= columnCount; ++c) {
                row.add(resultSet.getString(c).intern());
            }
        }
        return table;
    }

    public static ArrayList resultSetToArrayList(ResultSet rs) {
        try {

            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            ArrayList results = new ArrayList();

            while (rs.next()) {
                HashMap row = new HashMap();
                results.add(row);
                for (int i = 1; i <= columns; i++) {
                    row.put(md.getColumnLabel(i), rs.getObject(i));
                }
            }
            return results;
        } catch (Exception ex) {
            return null;
        }
    }

    public static void UploadFile(HttpServletRequest request) {
        try {
            String contentType = request.getContentType();
            System.out.println("Content type is :: " + contentType);
            if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
                DataInputStream in = new DataInputStream(request.getInputStream());
                int formDataLength = request.getContentLength();

                byte dataBytes[] = new byte[formDataLength];
                int byteRead = 0;
                int totalBytesRead = 0;
                while (totalBytesRead < formDataLength) {
                    byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                    totalBytesRead += byteRead;
                }

                String file = new String(dataBytes);
                String saveFile = file.substring(file.indexOf("filename=\"") + 10);
                saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
                saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));

//out.print(dataBytes);

                int lastIndex = contentType.lastIndexOf("=");
                String boundary = contentType.substring(lastIndex + 1, contentType.length());
//out.println(boundary);
                int pos;
                pos = file.indexOf("filename=\"");

                pos = file.indexOf("\n", pos) + 1;

                pos = file.indexOf("\n", pos) + 1;

                pos = file.indexOf("\n", pos) + 1;


                int boundaryLocation = file.indexOf(boundary, pos) - 4;
                int startPos = ((file.substring(0, pos)).getBytes()).length;
                int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

                 File dir1 = new File (".");
               String path1 =   dir1.getAbsolutePath();
                 String path2 =   dir1.getCanonicalPath();


                FileOutputStream fileOut = new FileOutputStream("C:/products/"+saveFile);

                //fileOut.write(dataBytes);
                fileOut.write(dataBytes, startPos, (endPos - startPos));
                fileOut.flush();
                fileOut.close();

            }
        } catch (Exception ex) {
            String error = ex.getMessage();
        }
    }
}
